package hu.szyrt.bug;

import hu.szyrt.bug.db.DBHandler;
import hu.szyrt.bug.models.PlaceItem;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class UseDealSuccessActivity extends Activity {

	public final static String  PLACE_ID 	 = "place_id",
	   							VOUCHER_ID   = "voucher_id";
	
	DBHandler dbHandler;
	PlaceItem placeItem;
	String voucherID = null;
	
	
	TextView nameTextView,
			 dealCountTextView,
			 dealDescriptionTextView,
			 voucherIDTextView;
	ImageButton okButton;
			 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.deal_use_success);
		
		dbHandler = new DBHandler(this);
		
		boolean hasPlaceID = getIntent().getExtras() != null && getIntent().getExtras().containsKey(PLACE_ID);
		int placeID = hasPlaceID ? getIntent().getExtras().getInt(PLACE_ID) : -1;
		
		if(placeID <= 0){ finish(); return; }				
		if(hasPlaceID) placeItem = dbHandler.loadPlace(placeID, false);
		if(getIntent().getExtras().containsKey(VOUCHER_ID)) voucherID = getIntent().getExtras().getString(VOUCHER_ID);
		
		if(placeItem == null || voucherID == null){
			finish();
			return;
		}		
		
		nameTextView = (TextView)findViewById(R.id.name);
		nameTextView.setText(placeItem.getName());
		
		dealCountTextView = (TextView)findViewById(R.id.deal_count);
		dealCountTextView.setText(placeItem.getDiscountType() == 2 ? "COOL\nDEAL" : String.valueOf(placeItem.getDiscountValue() + "%"));
		dealCountTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, placeItem.getDiscountType() == 2 ? 26 : 32);
		
		dealDescriptionTextView	 = (TextView)findViewById(R.id.deal_description);
		dealDescriptionTextView.setText(placeItem.getDiscountDescription());
		
		voucherIDTextView = (TextView)findViewById(R.id.voucherID);
		voucherIDTextView.setText(voucherID);
		
		okButton = (ImageButton)findViewById(R.id.okButton);
		okButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		
	}
	
	public static void openActivity(Context context, int placeID, String voucherID){
		Intent i = new Intent(context, UseDealSuccessActivity.class);
			   i.putExtra(PLACE_ID, placeID);
			   i.putExtra(VOUCHER_ID, voucherID);
		context.startActivity(i);
	}
	
	
}
